{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c5044eb9-4dea-4525-a9b7-bd951b85511d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nearest neighbor: (9, None, None, array([1.5, 2.5, 3.5], dtype=float32))\n"
     ]
    }
   ],
   "source": [
    "import psycopg2\n",
    "import numpy as np\n",
    "from pgvector.psycopg2 import register_vector\n",
    "\n",
    "# Connect to the database\n",
    "conn = psycopg2.connect(\"dbname=postgres user=hbu host=localhost\")\n",
    "register_vector(conn)\n",
    "cur = conn.cursor()\n",
    "\n",
    "# Insert a vector\n",
    "embedding = np.array([1.5, 2.5, 3.5])\n",
    "cur.execute(\"INSERT INTO items (embedding) VALUES (%s)\", (embedding.tolist(),))\n",
    "\n",
    "# Perform a similarity search\n",
    "query_vector = np.array([2, 3, 4])\n",
    "#query_vector = [2, 3, 4]\n",
    "cur.execute(\"SELECT * FROM items ORDER BY embedding <-> %s LIMIT 1\", (query_vector,))\n",
    "result = cur.fetchone()\n",
    "print(f\"Nearest neighbor: {result}\")\n",
    "\n",
    "conn.commit()\n",
    "cur.close()\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "05b22e9b-8361-49ac-a2ac-5f310ab07244",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nearest neighbor: (9, None, None, array([1.5, 2.5, 3.5], dtype=float32))\n"
     ]
    }
   ],
   "source": [
    "import psycopg\n",
    "import numpy as np\n",
    "from pgvector.psycopg import register_vector\n",
    "\n",
    "# Connect to the database\n",
    "conn = psycopg.connect(\"dbname=postgres user=hbu host=localhost\")\n",
    "register_vector(conn)\n",
    "cur = conn.cursor()\n",
    "query_vector = np.array([2, 3, 4])\n",
    "cur.execute(\"SELECT * FROM items ORDER BY embedding <-> %s LIMIT 1\", (query_vector,))\n",
    "result = cur.fetchone()\n",
    "print(f\"Nearest neighbor: {result}\")\n",
    "conn.commit()\n",
    "cur.close()\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a55027ae-b08e-4da3-b9b2-351291c4f7e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Alice!\n",
      "Hello, Alice!\n",
      "Hello, Alice!\n"
     ]
    }
   ],
   "source": [
    "def repeat(n):\n",
    "    def decorator(func):\n",
    "        def wrapper(*args, **kwargs):\n",
    "            for _ in range(n):\n",
    "                result = func(*args, **kwargs)\n",
    "            return result\n",
    "        return wrapper\n",
    "    return decorator\n",
    "\n",
    "@repeat(3)\n",
    "def greet(name):\n",
    "    print(f\"Hello, {name}!\")\n",
    "\n",
    "greet(\"Alice\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4edb8c9b-23e0-4bf0-b005-a0a5e8453831",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
